package it.unibo.deis.lia.mmhc.tcf;

import java.util.Vector;

public class ConnectionManager extends Thread {

	private Vector<Connection> connessioni;

	public ConnectionManager() {
		super();
		this.connessioni = new Vector<Connection>();
	}

	private static ConnectionManager connectionManager = null;

	public synchronized static ConnectionManager getInstance() throws Exception {
		if (connectionManager == null) {
			connectionManager = new ConnectionManager();
			connectionManager.start();
			return connectionManager;
		}
		return connectionManager;
	}

	public boolean addConnection(Connection con) {
		
		boolean trovato=false;
		for (int i = 0; i < connessioni.size(); i++) {
			if (connessioni.elementAt(i).getRemoteIP().equalsIgnoreCase(
					con.getRemoteIP()) && connessioni.elementAt(i).getLocalIp()
					.equalsIgnoreCase(con.getLocalIp())) {
				trovato=true;
				break;
			}
		}
		if(!trovato){
			connessioni.add(con);
			return true;
		}
		else{
			return false;
		}
		
	}

	public Connection getConnection(String remoteIp, String localIP) {
		for (int i = 0; i < connessioni.size(); i++) {
			if (connessioni.elementAt(i).getRemoteIP().equalsIgnoreCase(
					remoteIp)
					&& connessioni.elementAt(i).getLocalIp().equalsIgnoreCase(
							localIP)) {
				return connessioni.elementAt(i);
			}
		}
		return null;
	}

	public Vector<Connection> getConnessioni() {
		return connessioni;
	}

	public void run() {

	}
}
